#include "ConnectLine.h"
#include "GraphNode.h"
#include"GraphBase.h"
#define SetAttr(AttrName) SetAttribute(AttrName, ele->Attribute(AttrName));
void ConnectLine::ParseXmlElement(TiXmlElement* ele)
{
    this->name = ele->Attribute("name");
    SetAttr("pos1")
        SetAttr("pos2")
        SetAttr("size")
        SetAttr("content")
}

bool ConnectLine::SetAttribute(const char* attrName, const char* attrVal)
{
    if (attrVal == nullptr) return false;
	if (Control::SetAttribute(attrName, attrVal)) {
		return true;
	}
    if (strcmp(attrName, "pos1") == 0)
    {
        int x, y;
        sscanf_s(attrVal, "%d,%d", &x, &y);
        p1.x = x;
        p1.y = y;
        return true;
    }
    if (strcmp(attrName, "pos2") == 0)
    {
        int x, y;
        sscanf_s(attrVal, "%d,%d", &x, &y);
        p2.x = x;
        p2.y = y;
        return true;
    }
    return false;
}
void ConnectLine::onMouseButtonDown(int button, int x, int y)
{
    if (!bVisible) return;
    p2.x.val = x;
    p2.y.val = y;
    printf("%d,%d    ", x, y);
}
BlueprintScriptLibrary::Class* ConnectLine::GetClass()
{
    Array<NativeMembers> arrMember(4);
    Array<MethodPin> arrMethod(4, false);
    return BlueprintScriptLibrary::Class::BuildClassFromArray("TextInputBox", arrMember, arrMethod);
}
void ConnectLine::Connect(GraphNode* cg0, GraphNode* cg1)
{
    g0 = cg0;
    g1 = cg1;
    g0->fp->ConnectTo(g1->fp);
    g0->nextGraph = g1;

}
void ConnectLine::ConnectToGraphBase(GraphBase* cg0, GraphBase* cg1)
{
    gb0 = cg0;
    gb1 = cg1;
  /*  gb0->fp->ConnectTo(g1->fp);
    gb0->nextGraph = g1;*/
}
void ConnectLine::OnDraw()
{
    if (!bVisible) return;
    glColor4f(1, 1, 1, 1);
    // glColor3f(1, 1, 1);
    glLineWidth(4);
    glPushMatrix();
    glTranslatef(baseOffset.x.val, baseOffset.y.val, 0);
    if (NeedDebugDraw)
    {
        glBegin(GL_LINES);
        glVertex2i(p1.x.val, p1.y.val);
        glVertex2i(p2.x.val, p2.y.val);
        glEnd();
    }
    if (g0 != nullptr && g1 != nullptr)
    {
        p1.x.val = g0->pos.x.val + g0->size.x.val;
        p1.y.val = g0->pos.y.val + g0->size.y.val / 2;
        p2.x.val = /*baseOffset.x.val +*/ g1->pos.x.val;
        p2.y.val = /*baseOffset.y.val +*/ g1->pos.y.val + g1->size.y.val / 2;
    } if (gb0 != nullptr && gb1 != nullptr)
    {
        p1 =gb0->GetOutputPinPos(0);
        p2 =gb1->GetInputPinPos(0);
        //p1.x.val = gb0->pos.x.val + gb0->size.x.val;
        //p1.y.val = gb0->pos.y.val + gb0->size.y.val / 2;
        //p2.x.val = /*baseOffset.x.val +*/ gb1->pos.x.val;
        //p2.y.val = /*baseOffset.y.val +*/ gb1->pos.y.val + gb1->size.y.val / 2;
    }
    Vec2vbi p11, p12;
    // p11.x = p12.x = (p1.x.val + p2.x.val) / 2;
    int off = (p2.x.val - p1.x.val) / 2;
    p11.x = p1.x.val + abs(off);
    p12.x = p2.x.val - abs(off);
    p11.y = p1.y;
    p12.y = p2.y;
    int step = 20;//p2.x.val - p1.x.val;
    glBegin(GL_LINE_STRIP);
    for (int i = 0;i <= step;i++)
    {
        float t = (float)i / step;
        float x = pow((1. - t), 3.) * p1.x.val + 3. * t * pow((1. - t), 2.) * p11.x.val + 3 * pow(t, 2.) * (1 - t) * p12.x.val + pow(t, 3.) * p2.x.val;
        float y = pow((1. - t), 3.) * p1.y.val + 3. * t * pow((1. - t), 2.) * p11.y.val + 3 * pow(t, 2.) * (1 - t) * p12.y.val + pow(t, 3.) * p2.y.val;
        glVertex2f(x, y);
    }
    glEnd();
    glPopMatrix();

}